//
//  LoginViewController.m
//  OneDay
//
//  Created by lanou3g on 15/11/9.
//  Copyright © 2015年 oneDay. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userName;

@property (weak, nonatomic) IBOutlet UITextField *userPassword;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:205.91 / 255.0 green:239.77 / 255.0 blue:214.26 / 255.0 alpha:1];
    [self setNavigation];
    self.userPassword.secureTextEntry = YES;
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view from its nib.
}

#pragma mark -设置导航栏
- (void)setNavigation
{
    self.navigationItem.title = @"用户登录";
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(actionLeftButton:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    
}

#pragma mark -实现导航栏按钮触发方法
- (void)actionLeftButton:(UIBarButtonItem *)leftButton
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)loginButton:(UIButton *)sender {
    if ([[DataManager shareManager] isLogin] == YES) {
        [self showAlterViewWithTitle:@"提示" Message:@"您已经登录"];
    }else{
        [AVUser logInWithUsernameInBackground:self.userName.text password:self.userPassword.text block:^(AVUser *user, NSError *error) {
            if ([self.userName.text isEqualToString:@""] || [self.userPassword.text isEqualToString:@""]) {
                [self showAlterViewWithTitle:@"提示" Message:@"用户名或者密码不能为空"];
            }else{
                // 如果错误
                if (error) {
                    NSInteger codeNum = [[error.userInfo valueForKey:@"code"] integerValue];
                    if (codeNum == 210){
                        [self showAlterViewWithTitle:@"提示" Message:@"用户名或者密码错误"];
                    }
                }else{
                    // 登陆成功
                    // 持久化账号密码
                    [[DataManager shareManager] userName:self.userName.text];
                    [[DataManager shareManager] userPassword:self.userPassword.text];
                    // 更改登录状态
                    [[DataManager shareManager] isLogin:YES];
                    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"登录成功" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"进入OneDay", nil];
                    [alertView show];
                    // 调用一个成功的block
                    self.block();
                }
            }
        }];
        
    }
    
}

#pragma marks -- UIAlertViewDelegate --
// 进入OneDay
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

// 注册新用户
- (IBAction)registerNewUser:(UIButton *)sender {
    RegisterViewController *registVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registVC animated:YES];
    
}

// 封装一个alertView
- (void)showAlterViewWithTitle:(NSString *)title Message:(NSString *)message
{
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [self performSelector:@selector(closeAlter:) withObject:alertView afterDelay:1];
    
    [self presentViewController:alertView animated:YES completion:nil];
}
// 设置1秒之后消失
- (void)closeAlter:(UIAlertController *)alert
{
    [alert dismissViewControllerAnimated:YES completion:nil];
    
}

// 点击页面空白出 收取键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
